package com.itdd.service.impl;

import com.itdd.mapper.UserMapper;
import com.itdd.pojo.User;
import com.itdd.service.UserService;
import com.itdd.service.utils.GetSqlSession;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Scanner;

public class UserServiceImpl implements UserService {

    @Override
    public void controlUser() {
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //定义一个输入
        Scanner Sca = new Scanner(System.in);
        do {
            //一个全部用户的集合
            List<User> allUser = userMapper.searchAll();
            System.out.println("这是全部用户的信息");
            for (int i = 0; i < allUser.size(); i++) {
                System.out.println(allUser.get(i));
            }
            System.out.println("请选择你想要执行的操作:1.删除账户\n2.添加账户");
            int judge = Sca.nextInt();
            if (judge == 1) {
                System.out.println("请输入你想要删除的用户号码:\n想退出按0");
                int ju = Sca.nextInt();
                if (ju == 0) break;
                System.out.println("你确定要删除吗,确定请输入“是”");
                if (Sca.next().equals("是")) {
                    userMapper.deleteByUserNum(ju);
                }
            } else if (judge == 2) {
                System.out.println("请依次输出你添加账户的账号，密码");
                User user1 = new User();
                user1.setUserAccount(Sca.nextInt());
                user1.setUserPassword(Sca.nextInt());
                userMapper.addUser(user1);
            } else {
                System.out.println("请输入指定的数字!");
            }
            System.out.println("如果想退出，请按0");
        } while (Sca.nextInt() != 0);
        sqlSession.close();
    }


    @Override
    public User login() {
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //定义一个输入
        Scanner Sca = new Scanner(System.in);
        int acc;
//输入账号和密码并检测
        while (true) {
            System.out.println("请依次输入你的账号和密码");
            acc = Sca.nextInt();
            int code = Sca.nextInt();
            if (userMapper.selectUserByMes(acc, code)) {
                System.out.println("账号密码正确!");
                break;
            } else {
                System.out.println("账号或者密码错误!请重新输入!");
            }
        }
//        sqlSession.close();
        return userMapper.searchByUserAccount(acc);
    }


}
